<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;
use vquery;
use app\model\zhanghao as zh;
use app\model\users as us;

/**
 * Description of Users
 *
 * @author Administrator
 */
class Users extends Check {

    public function __construct() {
        parent::__construct();
        $left = controller("index");
        $left->left();
    }

    public function addusers() {

        //获取所有权限
        $list = db("role")->select();
        $this->assign("list", $list);
        $title = "Dengeki Bunko AddUsers";
        session("cue", "添加用户");
        $this->assign("title", $title);
        return $this->fetch();
    }

    //保存用户
    public function save() {
        $data = input("post.");

//        
//
//
        $rule = [
            'u_username|帐户名' => 'require|max:15|unique:users',
            'u_password|密码' => 'require|max:15|alphaDash',
            'queren|确认密码' => 'require|max:15|confirm:u_password',
            'u_rid|权限' => 'require',
        ];
        $message = [
            'u_username.require' => '请填写账号名！',
            'u_username.max' => '用户名长度不正确！',
            'u_username.unique' => '用户名已存在！',
            'u_password.require' => '请填写密码！',
            'u_password.max' => '密码长度不正确！',
            'u_password.alphaDash' => '密码只允许字母数字下划线！',
            'queren.require' => '请填写确认密码！',
            'queren.max' => '确认密码长度不正确！',
            'queren.confirm' => '两次密码不一致',
            'u_rid.require' => '请选择权限！',
        ];
//
        $res = $this->validate($data, $rule, $message);
//
        if (true !== $res) {

            return ["status" => -1, 'message' => $res];
        }
        $data['u_password'] = md5(input("post.u_password"));
        $data['u_status'] = 1;

        if (us::create($data)) {
            return ["status" => 1, 'message' => "添加用户成功！"];
        } else {
            return ["status" => 2, 'message' => "添加用户失败，用户名可能已重复！"];
        }
    }

}
